package com.cofi.vueelementadminserver.controller.system;

import com.alibaba.fastjson2.JSON;
import com.cofi.vueelementadminserver.config.security.SecurityUserDetailsService;
import com.cofi.vueelementadminserver.entity.framework.VueElementAdminUserInfo;
import com.cofi.vueelementadminserver.entity.system.StmUser;
import com.cofi.vueelementadminserver.service.StmUserService;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xyh
 * @date 2024-12-26
 * @description
 */
@RestController
public class UserController {

    private final StmUserService userService;
    private final SecurityUserDetailsService securityUserDetailsService;
    public UserController(StmUserService userService,SecurityUserDetailsService securityUserDetailsService) {
        this.userService = userService;
        this.securityUserDetailsService = securityUserDetailsService;
    }

    @PostMapping("/login")
    public String login(@RequestBody Map<String,String> loginMap) {
        String username = loginMap.get("username");
        UserDetails userDetails = securityUserDetailsService.loadUserByUsername(username);
        return "login";
    }

    @RequestMapping("/doLogin")
    public String doLogin() {
        return "doLogin";
    }

    @RequestMapping("/user/login")
    public String test() {
        return "vue-user-login";
    }

    @RequestMapping("/test")
    public void test2(HttpServletResponse response) throws IOException {
        List<StmUser> list = userService.list();

        HashMap<String, Object> result = new HashMap<>();
        result.put("code",0);
        result.put("data",list);

        String resultJson = JSON.toJSONString(result);
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().println(resultJson);
    }

    @RequestMapping("/user/info")
    public Map<String, Object> info() {
        VueElementAdminUserInfo vueElementAdminUserInfo = new VueElementAdminUserInfo();
        vueElementAdminUserInfo.setId("110");
        vueElementAdminUserInfo.setAvatar("111");
        vueElementAdminUserInfo.setName("admin");
        vueElementAdminUserInfo.setIntroduction("系统管理员");
        vueElementAdminUserInfo.setRoles(Arrays.asList("admin","subSystemAdmin"));

        Map<String,Object> map = new HashMap<>();
        map.put("code",0);
        map.put("data",vueElementAdminUserInfo);


        return map;
    }

    @RequestMapping("/jwtTest")
    public String demo() {
        return "jwt test ok";
    }
}
